﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using Microsoft.Web.Preview.Services;
using Lx.Web.Common.UI;
using Lx.Web.Common.ControlsUI;
using Model;
using BLF.WWW_User.Teach;
using Model.WWW_User.Teach;
using System.IO;
using Model.WWW_User.User;

namespace WebServices.User
{
    /// <summary>
    /// Teach 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://www.italki.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class Teach : System.Web.Services.WebService
    {
        [WebMethod]
        public bool AddTeachProfile(string TeacherType, string TLang1, string TLang1Level, string TLang2, string TLang2Level, string TLang3,
            string TLang3Level, string TLang4, string TLang4Level, string TeachExper, string Price, bool FreeTrial, string VideoCode, string Address, string Address2,
            string ZipCode, string Telphone, string OpState, bool Isvalid, string ValidDate, string ValidM, string Week, string fromTime, string ToTime, string Timezone)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno != null && memberno != "")
            {
                TeachModel tm = new TeachModel();
                tm.MemberNo = memberno;
                tm.TeacherType = TeacherType;
                tm.TLang1 = TLang1;
                tm.TLang1Level = TLang1Level;
                tm.TLang2 = TLang2;
                tm.TLang2Level = TLang2Level;
                tm.TLang3 = TLang3;
                tm.TLang3Level = TLang3Level;
                tm.TLang4 = TLang4;
                tm.TLang4Level = TLang4Level;
                tm.TeachExper = short.Parse(TeachExper);
                tm.Price = decimal.Parse(Price);
                tm.FreeTrial = FreeTrial;
                tm.VideoCode = VideoCode;
                tm.Address = Address;
                tm.Address2 = Address2;
                tm.ZipCode = ZipCode;
                tm.Telphone = Telphone;
                tm.OpState = OpState;
                tm.Isvalid = Isvalid;
                tm.ValidDate = ValidDate == "" ? DateTime.Parse("1900-1-1") : DateTime.Parse(ValidDate);
                tm.ValidM = ValidM;
                tm.Week = Week;
                tm.FromTime = fromTime;
                tm.ToTime = ToTime;
                tm.Timezone = Timezone;
                return BLF.WWW_User.Teach.Teach.AddTeachProfile(tm);
            }
            else { return false; }
        }

        /// <summary>
        /// 添加申请老师资料2
        /// </summary>
        /// <param name="MotherTongue">母语</param>
        /// <param name="MotherTongueLevel">母语等级</param>
        /// <param name="MotherTongue2">母语2</param>
        /// <param name="MotherTongue2Level">母语2等级</param>
        /// <param name="MotherTongue3">母语3</param>
        /// <param name="MotherTongue3Level">母语3等级</param>
        /// <param name="MotherTongue4">母语4</param>
        /// <param name="MotherTongue4Level">母语4等级</param>
        /// <param name="Nickname">昵称</param>
        /// <param name="Nationality">来自国家</param>
        /// <param name="Hometown">故乡</param>
        /// <param name="Country">所在国家</param>
        /// <param name="City">城市</param>
        /// <param name="Gender">性别</param>
        /// <param name="BOD">出生日期</param>
        /// <param name="IsShowBod">是否显示年龄</param>
        /// <param name="Education">学历</param>
        /// <param name="OccupationCat">职业</param>
        /// <param name="Occupation">职业类型等级</param>
        /// <param name="IMType">名字</param>
        /// <param name="IMValue">姓氏</param>
        /// <param name="Address">地址</param>
        /// <param name="Address2">地址2</param>
        /// <param name="ZipCode">邮编</param>
        /// <param name="Telphone">电话</param>
        /// <returns>BOOL</returns>
        [WebMethod]
        public bool AddTeachProfile2(string memberno, string MotherTongue, string MotherTongueLevel, string MotherTongue2, string MotherTongue2Level, string MotherTongue3, string MotherTongue3Level, string MotherTongue4, string MotherTongue4Level,
            string Nickname, string Nationality, string Hometown, string Country, string City, string Gender, string BOD, bool IsShowBod, string Education, string OccupationCat, string Occupation, string IMType, string IMValue, string Address, string Address2, string ZipCode, string Telphone)
        {
            if (memberno != null && memberno != "")
            {
                return BLF.WWW_User.Teach.Teach.AddTeachProfile2(memberno, MotherTongue, MotherTongueLevel, MotherTongue2, MotherTongue2Level, MotherTongue3, MotherTongue3Level, MotherTongue4, MotherTongue4Level,
                Nickname, Nationality, Hometown, Country, City, Gender, BOD, IsShowBod, Education, OccupationCat, Occupation, IMType, IMValue, Address, Address2, ZipCode, Telphone);
            }
            else { return false; }
        }
        [WebMethod]
        /// <summary>
        /// 添加申请老师资料3
        /// </summary>
        /// <param name="memberno">会员号</param>
        /// <param name="Introduction">自我介绍</param>
        /// <param name="VideoCode">视频代码</param>
        /// <returns>BOOL</returns>
        public bool AddTeachProfile3(string Introduction, string VideoCode, string os)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno != null && memberno != "")
            {
                return BLF.WWW_User.Teach.Teach.AddTeachProfile3(memberno, Introduction, VideoCode, os);
            }
            else { return false; }
        }

        #region T vincent
        [WebMethod]
        public long AddCourse(string title, string desc, string languagelearn, string level, string baselanguage, string license, string ctype, decimal Price, bool IsProFree, bool IsProDiscount, string tags)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            CoursesModel cm = new CoursesModel();
            cm.MemberNo = memberno;
            cm.Title = title;
            cm.Desc = desc;
            cm.LanguageLearn = languagelearn;
            cm.Level = level;
            cm.BaseLanguage = baselanguage;
            cm.License = license;
            cm.CType = ctype;
            cm.Price = Price;
            cm.IsProFree = IsProFree;
            cm.IsProDiscount = IsProDiscount;
            if (memberno != null && memberno != "")
            {
                return BLF.WWW_User.Teach.Teach.AddCourse(cm, tags);
            }
            else { return (long)-1; }
        }
        [WebMethod]
        public int UpdateCourse(long cid, string title, string desc, string languagelearn, string level, string baselanguage, string license, string CType, decimal Price, bool IsProFree, bool IsProDiscount, string tags)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            CoursesModel cm = new CoursesModel();
            cm.MemberNo = memberno;
            cm.Title = title;
            cm.Desc = desc;
            cm.LanguageLearn = languagelearn;
            cm.Level = level;
            cm.BaseLanguage = baselanguage;
            cm.License = license;
            cm.CType = CType;
            cm.CID = cid;
            cm.Price = Price;
            cm.IsProFree = IsProFree;
            cm.IsProDiscount = IsProDiscount;
            if (memberno != null && memberno != "")
            {
                return BLF.WWW_User.Teach.Teach.UpdateCourse(cm, tags);
            }
            else { return 0; }
        }
        [WebMethod]
        public long AddLesson(string cid, string title, string desc, string longTime)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (string.IsNullOrEmpty(memberno)) return (long)-1;
            LessonModel lm = new LessonModel();
            lm.Title = title;
            lm.Desc = desc;
            lm.CID = long.Parse(cid);
            lm.LongTime = short.Parse(longTime);
            return BLF.WWW_User.Teach.Teach.AddLesson(lm, memberno);
        }
        [WebMethod]
        public bool UpdateLesson(long cid, long lid, string title, string desc, string longTime)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (string.IsNullOrEmpty(memberno)) return false;
            LessonModel lm = new LessonModel();
            lm.Title = title;
            lm.Desc = desc;
            lm.CID = cid;
            lm.LID = lid;
            lm.LongTime = short.Parse(longTime);
            return BLF.WWW_User.Teach.Teach.UpdateLesson(memberno, lm);
        }
        /// <summary>
        /// 添加sessions
        /// 0 sid
        /// 1 LChapter
        /// 2 教师的MemberCrtNo
        /// 3 教师的timezone
        /// 4 CTitle
        /// 5 LTitle
        /// </summary>
        /// <param name="sm">SessionsModel</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList AddSessions(long CID, long LID, string SNO, string TNO, string LanguageLearn, string BaseLanguage, string StartDate, string StartTime, short LongTime, string Price, string TeachTool, string AudioType, string SIM, string Message, string Timezone)
        {
            ArrayList arrlist=new ArrayList();
            string memberno = HttpContext.Current.User.Identity.Name;
            if (SNO != memberno || memberno == "")
            {
                arrlist.Add(-1);
                return arrlist;
            }
            SessionsModel sm = new SessionsModel();
            DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(StartDate, StartTime, Timezone);
            StartDate = dt.ToString("yyyyMMdd");
            StartTime = dt.ToString("HH:mm");
            sm.CID = CID;
            sm.LID = LID;
            sm.SNO = SNO;
            sm.TNO = TNO;
            sm.LanguageLearn = LanguageLearn;
            sm.BaseLanguage = BaseLanguage;
            sm.StartDate = StartDate;
            sm.StartTime = StartTime;
            sm.LongTime = LongTime;
            sm.Price = decimal.Parse(Price);
            sm.TeachTool = TeachTool;
            sm.AudioType = AudioType;
            arrlist = BLF.WWW_User.Teach.Teach.AddSessions(sm, SIM, Message);
            arrlist.Add(StartDate);//6
            arrlist.Add(StartTime);//7
            return arrlist;
        }
        [WebMethod]
        public bool SetSesState5(long sid, string memberno, string Message)
        {
            return BLF.WWW_User.Teach.Teach.SetSesState5(sid, memberno, Message);
        }
        [WebMethod]
        public bool SetSesState3(long sid, string memberno, string StartDate, string StartTime, decimal Price, string Message, string Timezone)
        {
            DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(StartDate, StartTime, Timezone);
            StartDate = dt.ToString("yyyyMMdd");
            StartTime = dt.ToString("HH:mm");
            return BLF.WWW_User.Teach.Teach.SetSesState3(sid, memberno, StartDate, StartTime, Price, Message);

        }
        /// <summary>
        /// 学生确定session变为0
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">学生会员号</param>
        /// <param name="Message">消息</param>
        /// <returns>bool</returns>
        [WebMethod]
        public bool SetSesState0(long cid,long sid, long LID, string SNO, string TNO, string LanguageLearn, string BaseLanguage, string StartDate, string StartTime, short LongTime, string TeachTool, string AudioType, string SIM, string Message, string Timezone, string State,string SessionType)
        {
            SessionsModel sm = new SessionsModel();
            DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(StartDate, StartTime, Timezone);
            StartDate = dt.ToString("yyyyMMdd");
            StartTime = dt.ToString("HH:mm");
            sm.SessionID = sid;
            sm.LID = LID;
            sm.SNO = SNO;
            sm.TNO = TNO;
            sm.CID=cid;
            sm.LanguageLearn = LanguageLearn;
            sm.BaseLanguage = BaseLanguage;
            sm.StartDate = StartDate;
            sm.StartTime = StartTime;
            sm.LongTime = LongTime;
            sm.TeachTool = TeachTool;
            sm.AudioType = AudioType;
            sm.State = State;
            sm.SessionType = SessionType;
            return BLF.WWW_User.Teach.Teach.SetSesState0(sm, SIM, Message);
        }
        /// <summary>
        /// 老师确定session变为B
        /// 0 为是否成功
        /// 1 学生email
        /// 2 学生的时区
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">学生会员号</param>
        /// <param name="Message">消息</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesStateB(long sid, string memberno)
        {
            return BLF.WWW_User.Teach.Teach.SetSesStateB(sid, memberno);
        }
        /// <summary>
        /// 老师确定session变为B
        /// 0 为是否成功
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">老师会员号</param>
        /// <param name="Message">解释</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesState8(long sid, string memberno, string Message)
        {
            return BLF.WWW_User.Teach.Teach.SetSesState8(sid, memberno, Message);
        }
        /// <summary>
        /// 学生确定session变为9或A
        /// 0 为是否成功
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">老师会员号</param>
        /// <param name="State">状态</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesState9A(long sid, string memberno, string State)
        {
            return BLF.WWW_User.Teach.Teach.SetSesState9A(sid, memberno, State);
        }
        /// <summary>
        /// 学生确定session变为F
        /// 0 为是否成功
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">学生会员号</param>
        /// <param name="Helpful">活跃评级</param>
        /// <param name="Knowledgeable">知识评级</param>
        /// <param name="Professional">专业评级</param>
        /// <param name="SToTDesc">学生对老师的评价</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesStateF(long sid, string memberno, short Professional, short Knowledgeable, short Helpful, string SToTDesc)
        {
            return BLF.WWW_User.Teach.Teach.SetSesStateF(sid, memberno, Professional, Knowledgeable, Helpful, SToTDesc);
        }
        /// <summary>
        /// 学生确定session变为C
        /// 0 为是否成功
        /// 1 老师的Email
        /// 2 老师的时区
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">学生会员号</param>
        /// <param name="Message">解释</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesStateC(long sid, string memberno, string Message)
        {
            return BLF.WWW_User.Teach.Teach.SetSesStateC(sid, memberno, Message);
        }
        /// <summary>
        /// 老师确定session变为H
        /// 0 为是否成功
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">老师会员号</param>
        /// <param name="Message">解释</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesStateH(long sid, string memberno, string Message)
        {
            return BLF.WWW_User.Teach.Teach.SetSesStateH(sid, memberno, Message);
        }
        /// <summary>
        /// 老师确定session变为G
        /// 0 为是否成功
        /// </summary>
        /// <param name="sid">sid</param>
        /// <param name="memberno">老师会员号</param>
        /// <param name="Message">评价</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList SetSesStateG(long sid, string memberno, string Message)
        {
            return BLF.WWW_User.Teach.Teach.SetSesStateG(sid, memberno, Message);
        }
        [WebMethod]
        public void SendSesState0(string email, string sname, string tname, string StartDate, string StartTime, short LongTime, long sid, string Title, string timezone)
        {
                string HappenDate = DateTime.UtcNow.AddMinutes(double.Parse(timezone)+(Double)10080).ToString("MMM d, yyyy HH:mm", new System.Globalization.CultureInfo(Globals.Language));
                DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(StartDate, StartTime, timezone);
                StartDate = dt.ToString("MMM d, yyyy HH:mm", new System.Globalization.CultureInfo(Globals.Language)) + "-" + dt.AddMinutes((double)(LongTime * 30)).ToString("HH:mm");
                string link = System.Configuration.ConfigurationManager.AppSettings["WWWHost"] + "/session/session.htm?sid=" +sid.ToString();
                string subject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionState0o.xml", Globals.Language))), "email", "subject", "").Replace("$$SNickname$$", sname);
                string body = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionState0o.xml", Globals.Language))), "email", "body", "").Replace("$$TNickname$$", tname).Replace("$$SNickname$$", sname).Replace("$$Title$$", Title).Replace("$$StartDate$$", StartDate).Replace("$$HappenDate$$", HappenDate).Replace("$$link$$", link);
                if (System.Text.RegularExpressions.Regex.IsMatch(email, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
                {
                    MsMailSend.SendMail(subject, body, email, null, true);
                }
                return;
        }

        //[WebMethod]
        //public void SendSesState6(string email, string sname, string tname, string StartDate, string StartTime, long sid, string Title, string timezone)
        //{
        //    string HappenDate = "";
        //    DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(StartDate, StartTime, timezone);
        //    HappenDate=dt.ToString("MMM d, yyyy HH:mm", new System.Globalization.CultureInfo(Globals.Language));
           
        //    string link = System.Configuration.ConfigurationManager.AppSettings["WWWHost"] + "/session/session.htm?sid=" + sid.ToString();
        //    string subject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionState6o.xml", Globals.Language))), "email", "subject", "").Replace("$$SNickname$$", sname);
        //    string body = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionState6o.xml", Globals.Language))), "email", "body", "").Replace("$$TNickname$$", tname).Replace("$$SNickname$$", sname).Replace("$$HappenDate$$", HappenDate).Replace("$$link$$", link).Replace("$$SID$$", sid.ToString());
        //    MsMailSend.SendMail(subject, body, email, null, true);
        //    return;
        //}
        [WebMethod]
        public void SendSesStateB(string email, string sname, string tname, long sid, string timezone)
        {
            string HappenDate = DateTime.UtcNow.AddMinutes(double.Parse(timezone)+(Double)10080).ToString("MMM d, yyyy HH:mm", new System.Globalization.CultureInfo(Globals.Language));

            string link = System.Configuration.ConfigurationManager.AppSettings["WWWHost"] + "/session/session.htm?sid=" + sid.ToString();
            string subject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionStateBo.xml", Globals.Language))), "email", "subject", "").Replace("$$SID$$", sid.ToString());
            string body = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionStateBo.xml", Globals.Language))), "email", "body", "").Replace("$$TNickname$$", tname).Replace("$$SNickname$$", sname).Replace("$$HappenDate$$", HappenDate).Replace("$$link$$", link).Replace("$$SID$$", sid.ToString());
            if (System.Text.RegularExpressions.Regex.IsMatch(email, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
            {
                MsMailSend.SendMail(subject, body, email, null, true);
            }
            return;
        }
        [WebMethod]
        public void SendSesStateC(string email, string sname, string tname, long sid, string timezone)
        {
            string HappenDate = DateTime.UtcNow.AddMinutes(double.Parse(timezone) + (double)20160).ToString("MMM d, yyyy HH:mm", new System.Globalization.CultureInfo(Globals.Language));

            string link = System.Configuration.ConfigurationManager.AppSettings["WWWHost"] + "/session/session.htm?sid=" + sid.ToString();
            string subject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionStateCo.xml", Globals.Language))), "email", "subject", "").Replace("$$SID$$", sid.ToString());
            string body = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionStateCo.xml", Globals.Language))), "email", "body", "").Replace("$$TNickname$$", tname).Replace("$$SNickname$$", sname).Replace("$$HappenDate$$", HappenDate).Replace("$$link$$", link).Replace("$$SID$$", sid.ToString());
            if (System.Text.RegularExpressions.Regex.IsMatch(email, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
            {
                MsMailSend.SendMail(subject, body, email, null, true);
            }
            return;
        }
        public string FormatFileName(string filename, string replaceLang)
        {
            if (!File.Exists(filename))
            {
                filename = filename.Replace("\\" + replaceLang + "\\", "\\en-us\\");
            }
            return filename;
        }
        public string FormatFileName(string filename)
        {
            if (!File.Exists(filename))
            {
                filename = filename.Replace("\\" + Lx.Web.Common.UI.Globals.Language + "\\", "\\en-us\\");
            }
            return filename;
        }
        /// <summary>
        /// 得到投诉和辩解内容
        /// 0 学生投诉的内容
        /// 1 为老师辩解的理由
        /// </summary>
        /// <param name="sid">sid</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList GetDispute(long sid)
        {
            return BLF.WWW_User.Teach.Teach.GetDispute(sid);
        }
        /// <summary>
        /// 添加老师的URL证据
        /// </summary>
        /// <param name="memberno">管理员号</param>
        /// <param name="TeachURL">老师提供的URL</param>
        /// <param name="sid">相应的sid</param>
        /// <returns>bool</returns>
        [WebMethod]
        public bool AddTeachURL(string TeachURL, long sid)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return false;
            }
            return BLF.WWW_User.Teach.Teach.AddTeachURL(memberno, TeachURL, sid);
        }
        /// <summary>
        /// 得到会员的空闲时间列表
        /// </summary>
        /// <param name="memberno">会员号</param>
        /// <returns>List（UserTimeAvailability）</returns>
        [WebMethod]
        public ArrayList GetUserTime(string memberno, string timezone)
        {
            ArrayList arrlist = new ArrayList();
            List<UserTimeAvailability> utalist;
            timezone = timezone == "" ? "0" : timezone;
            utalist=BLF.WWW_User.Teach.Teach.UserTimeAvaila(BLF.WWW_User.Teach.Teach.GetUserTime(memberno),timezone);
            arrlist.Add(utalist);
            try
            {
                
                string ti =GetTimezoneText(timezone);
                string[] sText = new string[] { Lx.Web.Common.Resource.GetText(Globals.Language, "TP015"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP017"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP018"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP019"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP020"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP021"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP022"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP023"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP248"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "TP249"),
                ti          
            };
                arrlist.Add(sText);
            }
            catch (Exception)
            {
                
                throw;
            }
           
            
            //arrlist.Add(sText);
            return arrlist;
        }
        protected DataTable GetDTFromXml(string fileName)
        {
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(Server.MapPath(String.Format("/Control/{0}/{1}", Lx.Web.Common.UI.Globals.Language, fileName)));
                return ds.Tables[0];
            }
            catch
            {
                ds.ReadXml(Server.MapPath(String.Format("/Control/en-us/{0}", fileName)));
                return ds.Tables[0];
            }
        }
        private string GetTextFromDT(DataTable dt, string value)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[0].ToString().ToLower() == value.ToLower())
                {
                    return dr[1].ToString();
                }
            }
            return value;
        }
        public string GetTimezoneText(string value)
        {
            DataTable dt = GetDTFromXml("TimeZone.xml");
            return GetTextFromDT(dt, value);
        }
        /// <summary>
        /// MD5加密八位剪切 换位成4 2 1 3
        /// </summary>
        /// <param name="pass">密码</param>
        /// <returns>md5处理的</returns>
        [WebMethod]
        public string GetPassMd5(string pass)
        {
            pass=Globals.Enctype(pass);
            if(pass.Length==32)
            {
                pass = pass.Substring(24, 8) + pass.Substring(8, 8) + pass.Substring(0, 8) + pass.Substring(16, 8);
                return pass;
            }
            return "";
        }
        [WebMethod]
        public ArrayList GetMyLuSessions(string memberno, int pageIndex, int pageSize, string Timezone)
        {
            ArrayList arrlist = new ArrayList();
            arrlist = BLF.WWW_User.Teach.Teach.GetMyLuSessions(memberno, pageIndex, pageSize);
            if(arrlist!=null)
            {
                List<SessionsModel> semlist = new List<SessionsModel>();
                //处理过的
                List<SessionsModel> semlist1= new List<SessionsModel>();
                semlist = arrlist[0] as List<SessionsModel>;
                int lucount=(int)arrlist[1];
                arrlist.RemoveRange(0, arrlist.Count);
                System.Globalization.CultureInfo slan = new System.Globalization.CultureInfo(Lx.Web.Common.UI.Globals.Language);
                string lan = Globals.Language;
                string TP350 = Lx.Web.Common.Resource.GetText(lan, "TP350");
                string TP351 = Lx.Web.Common.Resource.GetText(lan, "TP351");
                string TP250 = Lx.Web.Common.Resource.GetText(lan, "TP250");
                foreach (SessionsModel sm in semlist)
                {
                    bool st = false;
                    st = GetUpstate(sm.StartDate, sm.StartTime, sm.LongTime,Timezone);
                    //保存图片
                    sm.AudioType = st ? "<img src='/images/icon_live.gif' align='absmiddle' title='[LIVE]' />" : "<img scr='/Images/icon_upcoming.gif' align='absmiddle' title='[UPCOMING]'/>";
                    //保存状态显示的文字
                    sm.SToTDesc = st ? TP350 : TP351;
                    sm.SessionType = sm.SessionType == "1" ? string.Format("<a href='/lesson/" + sm.LID.ToString() + "'>" + Lx.Web.Common.Resource.GetText(lan, "TP406", new string[] { sm.LID.ToString(), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.CTitle), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.LTitle) }) + "</a>") : TP250;
                    //保存发生时间
                    sm.TToSDesc = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).ToString("MMM dd,yyyy HH:mm", slan) + "-" + BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).AddMinutes((double)((sm.LongTime * 30))).ToString("HH:mm");
                    semlist1.Add(sm);
                }
                arrlist.Add(semlist1);
                arrlist.Add(new BLF.PageableObject(lucount, pageIndex, pageSize));
                return arrlist;
            }
            return null; 
        }
        [WebMethod]
        public ArrayList GetMyUpSessions(string memberno, int pageIndex, int pageSize,string Timezone)
        {
            ArrayList arrlist = new ArrayList();
            arrlist = BLF.WWW_User.Teach.Teach.GetMyUpSessions(memberno, pageIndex, pageSize);
            if (arrlist != null)
            {
                List<SessionsModel> semlist = new List<SessionsModel>();
                //处理过的
                List<SessionsModel> semlist1 = new List<SessionsModel>();
                semlist = arrlist[0] as List<SessionsModel>;
                int lucount = (int)arrlist[1];
                arrlist.RemoveRange(0, arrlist.Count);
                System.Globalization.CultureInfo slan = new System.Globalization.CultureInfo(Lx.Web.Common.UI.Globals.Language);
                string lan=Globals.Language;
                string TP353 = Lx.Web.Common.Resource.GetText(lan, "TP353");
                string TP352 = Lx.Web.Common.Resource.GetText(lan, "TP352");
                string TP250 = Lx.Web.Common.Resource.GetText(lan, "TP250");
                foreach (SessionsModel sm in semlist)
                {
                    //保存图片
                    sm.AudioType = "";
                    //保存状态显示的文字
                    sm.SToTDesc = sm.DescDate.Subtract(sm.HappenDate).TotalDays > 7 ? TP353 : TP352;
                    sm.SessionType = sm.SessionType == "1" ? string.Format("<a href='/lesson/" + sm.LID.ToString() + "'>" + Lx.Web.Common.Resource.GetText(lan, "TP406", new string[] { sm.LID.ToString(), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.CTitle), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.LTitle) }) + "</a>") : TP250;
                    //保存发生时间
                    sm.TToSDesc = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).ToString("MMM dd,yyyy HH:mm", slan) + "-" + BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).AddMinutes((double)((sm.LongTime * 30))).ToString("HH:mm");
                    semlist1.Add(sm);
                }
                arrlist.Add(semlist1);
                arrlist.Add(new BLF.PageableObject(lucount, pageIndex, pageSize));
                return arrlist;
            }
            return null; 
        }
        [WebMethod]
        public ArrayList GetMyPsSessions(string memberno, int pageIndex, int pageSize, string Timezone)
        {
            ArrayList arrlist = new ArrayList();
            arrlist = BLF.WWW_User.Teach.Teach.GetMyPsSessions(memberno, pageIndex, pageSize);
            if (arrlist != null)
            {
                List<SessionsModel> semlist = new List<SessionsModel>();
                //处理过的
                List<SessionsModel> semlist1 = new List<SessionsModel>();
                semlist = arrlist[0] as List<SessionsModel>;
                int lucount = (int)arrlist[1];
                arrlist.RemoveRange(0, arrlist.Count);
                System.Globalization.CultureInfo slan = new System.Globalization.CultureInfo(Lx.Web.Common.UI.Globals.Language);
                string lan = Globals.Language;
                string TP354 = Lx.Web.Common.Resource.GetText(lan, "TP354");
                string TP250 = Lx.Web.Common.Resource.GetText(lan, "TP250");
                foreach (SessionsModel sm in semlist)
                {
                    //保存图片
                    sm.AudioType = "";
                    //保存状态显示的文字
                    sm.SToTDesc = TP354;
                    sm.SessionType = sm.SessionType == "1" ? string.Format("<a href='/lesson/" + sm.LID.ToString() + "'>" + Lx.Web.Common.Resource.GetText(lan, "TP406", new string[] { sm.LID.ToString(), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.CTitle), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.LTitle) }) + "</a>") : TP250;
                    //保存发生时间
                    sm.TToSDesc = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).ToString("MMM dd,yyyy HH:mm", slan) + "-" + BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).AddMinutes((double)((sm.LongTime * 30))).ToString("HH:mm");
                    semlist1.Add(sm);
                }
                arrlist.Add(semlist1);
                arrlist.Add(new BLF.PageableObject(lucount, pageIndex, pageSize));
                return arrlist;
            }
            return null; 
        }

        private bool GetUpstate(string StartDate, string StartTime, short LongTime, string Timezone)
        {
            try
            {
                DateTime dateTime = new DateTime(int.Parse(StartDate.Substring(0, 4)), int.Parse(StartDate.Substring(4, 2)), int.Parse(StartDate.Substring(6, 2)));
                double h = 0;
                double m = 0;
                string[] strSp = StartTime.Split(':');
                if (strSp.Length > 1)
                {
                    double.TryParse(strSp[0], out h);
                    double.TryParse(strSp[1], out m);
                }
                dateTime = dateTime.AddMinutes(h * 60 + m);
                DateTime datetime1 = dateTime.AddMinutes((double)(LongTime * 30));
                DateTime datetime2 = DateTime.UtcNow;
                if (dateTime <= datetime2 && datetime1 >= datetime2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region hui
        /// <summary>
        /// 添加并获取session消息 0：CommentModel List 消息列表
        /// </summary>
        /// <param name="sid">session id</param>
        /// <param name="msg">消息</param>
        /// <returns>ArrayList</returns>
        [WebMethod]
        public ArrayList AddGetMessage(long sid, string msg)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return null;
            }
            return BLF.WWW_User.Teach.Teach.AddGetMessage(memberno, sid, HttpUtility.HtmlEncode(msg));
        }
        /// <summary>
        /// 获取lesson内容
        /// </summary>
        /// <param name="lid">lesson id</param>
        /// <returns>string</returns>
        [WebMethod]
        public string GetLessonDesc(long lid)
        {
            return BLF.WWW_User.Teach.Teach.GetLessonDesc(lid);
        }
        /// <summary>
        /// 获取某个会员的course列表信息（请求课程）
        /// 0：CoursesModel list
        /// 1：LessonModel lesson第一个course下的lesson列表
        /// </summary>
        /// <param name="tNO">教师编号</param>
        /// <returns>CoursesModel 列表</returns>        
        [WebMethod]
        public ArrayList GetUserCourses(string tNO)
        {
            return BLF.WWW_User.Teach.Teach.GetUserCourses(tNO);
        }
        /// <summary>
        /// 获取某个course的lesson列表信息（请求课程）        
        /// </summary>
        /// <param name="tNO">教师编号</param>
        /// <param name="cid">course id</param>
        /// <returns>lesson列表</returns>
        [WebMethod]
        public List<LessonModel> GetACourseLessons(string tNO, string cid)
        {
            return BLF.WWW_User.Teach.Teach.GetACourseLessons(tNO, long.Parse(cid));
        }
        /// <summary>
        /// 获取教师语言信息（请求辅导）
        /// </summary>
        /// <param name="tNO">教师编号</param>
        /// <returns>TeachModel</returns>
        [WebMethod]
        public TeachModel GetRequestTutorInfo(string tNO)
        {
            return BLF.WWW_User.Teach.Teach.GetRequestTutorInfo(tNO);
        }
        /// <summary>
        /// 添加session(学生申请)        
        /// </summary>
        [WebMethod]
        public ArrayList AddSession(long cid, long lid, string tNO, string ll, string bl, string startDate, string startH,string startM, short longTime, string price, string teachTool, string audioType, string sIM, string msg,string sTimezone)
        {
            ArrayList arrlist = null;
            string memberno = HttpContext.Current.User.Identity.Name;
            if (tNO == memberno || memberno == "")
            {
                return arrlist;
            }            
            string startTime = startH + ":" + startM;
            sTimezone = "-" + sTimezone;
            DateTime sDT = BLF.WWW_User.Teach.Teach.ConvertTime(startDate, startTime, sTimezone);
            if (sDT < DateTime.UtcNow)//判断上课时间
            {
                arrlist = new ArrayList();
                arrlist.Add("TimeError");
                return arrlist;
            }           
           
            SessionsModel sm = new SessionsModel();            
            sm.CID = cid;
            sm.LID = lid;
            sm.SNO = memberno;
            sm.TNO = tNO;
            sm.LanguageLearn = ll;
            sm.BaseLanguage = bl;
            sm.StartDate = sDT.ToString("yyyyMMdd");
            sm.StartTime = sDT.ToString("HH:mm"); ;
            sm.LongTime = longTime;
            sm.Price = decimal.Parse(price);
            sm.TeachTool = teachTool;
            sm.AudioType = audioType;
            arrlist = BLF.WWW_User.Teach.Teach.AddSession(sm, sIM, HttpUtility.HtmlEncode(msg));
            if (arrlist != null && arrlist[0].ToString() != "0")
            {
                BaseControl baseControl = new BaseControl();
                sm = arrlist[1] as SessionsModel;
                string subject = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionState0o.xml", sm.BaseLanguage))), "email", "subject", "");
                string body = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/SessionState0o.xml", sm.BaseLanguage))), "email", "body", "");
                
                new System.Threading.Thread(new System.Threading.ThreadStart(delegate
                {                    
                    string timezone = (string)arrlist[3];
                    string title = "";
                    if (cid > 0)
                        title = Lx.Web.Common.Resource.GetText(sm.BaseLanguage,"TP405", new string[] { sm.LChapter.ToString(), sm.LTitle, sm.CTitle });
                    else
                        title = Lx.Web.Common.Resource.GetText(sm.BaseLanguage,"TP250");
                    DateTime happenDT = DateTime.UtcNow.AddMinutes(double.Parse(timezone) + (double)10080);
                    DateTime dt = BLF.WWW_User.Teach.Teach.ConvertTime(startDate, startTime, timezone);
                    string HappenDate = "";
                    if (happenDT > dt)
                        HappenDate = dt.ToString("MMM dd, yyyy HH:mm", new System.Globalization.CultureInfo(sm.BaseLanguage));
                    else
                        HappenDate = happenDT.ToString("MMM dd, yyyy HH:mm", new System.Globalization.CultureInfo(sm.BaseLanguage));
                    string start_endTime = dt.ToString("MMM dd, yyyy HH:mm", new System.Globalization.CultureInfo(sm.BaseLanguage)) + "-" + dt.AddMinutes((double)longTime * 30).ToString("HH:mm");
                    string link = string.Format("{0}/session/session.htm?sid={1}", System.Configuration.ConfigurationManager.AppSettings["WWWHost"], sm.SessionID);
                    subject = subject.Replace("$$SNickname$$", sm.SNickname);
                    body = body.Replace("$$TNickname$$", sm.TNickname).Replace("$$SNickname$$", sm.SNickname).Replace("$$Title$$", title).Replace("$$StartDate$$", start_endTime).Replace("$$HappenDate$$", HappenDate).Replace("$$link$$", link);
                    if (System.Text.RegularExpressions.Regex.IsMatch(arrlist[2].ToString(), "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
                    {
                        MsMailSend.SendMail(subject, body, arrlist[2].ToString(), null, true);
                    }
                })).Start();
            }
            return arrlist;
        }
        #endregion

        /// <summary>
        /// 修改老师的资料
        /// </summary>
        /// <param name="tm">老师模型</param>
        /// <returns>BOOL</returns>
        [WebMethod]
        public bool UpdateTeachProfile(string TLang1,string TLang2,string TLang3,string TLang4, string TLang1Level, string TLang2Level, 
            string TLang3Level, string TLang4Level, string TeachExper, string Price, bool FreeTrial, string VideoCode)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno != null && memberno != "")
            {
                TeachModel tm = new TeachModel();
                tm.MemberNo = memberno;
                tm.TLang1 = TLang1;
                tm.TLang1Level = TLang1Level;
                tm.TLang2 = TLang2;
                tm.TLang2Level = TLang2Level;
                tm.TLang3 = TLang3;
                tm.TLang3Level = TLang3Level;
                tm.TLang4 = TLang4;
                tm.TLang4Level = TLang4Level;
                tm.TeachExper = short.Parse(TeachExper);
                tm.Price = decimal.Parse(Price);
                tm.FreeTrial = FreeTrial;
                tm.VideoCode = VideoCode;
                return BLF.WWW_User.Teach.Teach.UpdateTeachProfile(tm);
            }
            else { return false; }
        }
    }
}
